#存储所有游戏运行的函数
import sys
import pygame
from bullet import Bullet
from alien import Ailen
import time
#开火
def fire_bullet(ai_settings,screen,ship,bullets):
    if len(bullets)<ai_settings.bullets_allowed:
        #创建一个子弹对象
        new_bullet = Bullet(ai_settings,screen,ship)
        #往数组中添加一个子弹对象
        bullets.add(new_bullet)

def check_keydown_events(event,ai_settings,screen,ship,bullets):
    if event.key == pygame.K_RIGHT:
        ship.moving_right = True
    if event.key == pygame.K_LEFT:
        ship.moving_left = True
    if event.key == pygame.K_SPACE:
        fire_bullet(ai_settings,screen,ship,bullets)


def check_keyup_events(event,ship):
    if event.key == pygame.K_RIGHT:
        ship.moving_right = False
    if event.key == pygame.K_LEFT:
        ship.moving_left = False

#左右键，实现飞船移动
def check_events(ai_settings,screen,stats,sb,play_button,ship,aliens, bullets):
    #检测所有事件
    for event in  pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
        elif event.type == pygame.KEYDOWN:
            check_keydown_events(event,ai_settings,screen,ship,bullets)
            if event.key ==pygame.K_m:
                ai_settings.bullet_width = 400
            if event.key ==pygame.K_n:
                ai_settings.bullet_width = 3
        elif event.type == pygame.KEYUP:
            check_keyup_events(event,ship)
        elif event.type == pygame.MOUSEBUTTONDOWN:
            #获得按下是的x,y
            mouse_x,mouse_y =   pygame.mouse.get_pos()
            check_play_button(ai_settings,screen,stats,sb,play_button,ship,aliens, bullets,mouse_x,mouse_y)

def check_play_button(ai_settings,screen,stats,sb,play_button,ship,aliens, bullets,mouse_x,mouse_y):
    if play_button.rect.collidepoint(mouse_x,mouse_y) and stats.game_active==False:
        #隐藏光标
        pygame.mouse.set_visible(False)
        #重置游戏速度
        ai_settings.init_dynamic_settings()
        #重置游戏状态
        stats.game_active = True
        stats.resset_stats()
        #重置记分版
        sb.prep_score()
        sb.prep_height_score()
        sb.prep_level()
        #重置飞船
        sb.prep_ships()

        #清空现有游戏对象
        bullets.empty()
        aliens.empty()
        #创建新外星人
        create_fleet(ai_settings,screen,ship,aliens)
        ship.center_ship()

def update_screen(ai_settings,screen,stats,ship,bullets,aliens,play_button,sb):
    #设置背景色
    screen.fill(ai_settings.bg_color)
    #画出得分
    sb.show_score()
    #画出子弹
    for bullet in bullets.sprites():
        bullet.draw_bullet()
    #画出飞船
    ship.blitme()
    #画出外星人
    #aliens.draw(screen)
    for alien in aliens:
        alien.blitme()
    #绘制按钮
    if stats.game_active==False:
        play_button.draw_button()
        #隐藏光标
        pygame.mouse.set_visible(True)
    #显示屏幕
    pygame.display.flip()

def check_bullet_aliens_conllisions(ai_settings,screen,stats,sb,ship,aliens,bullets):
    #碰撞检测，如果碰撞到了 子弹是key 外星人是value
    conllisions = pygame.sprite.groupcollide(bullets,aliens,True,True)
    #{"bullet",[alien1,alien2,alien3,alien4]}
    #{"bullet",alien1}
    if conllisions:
        for aliens in conllisions.values():
            stats.score = stats.score+ai_settings.alien_points*len(aliens)
            sb.prep_score()
        #检查有没有超过最高分
        check_height_score(stats,sb)
    #外星人数组为0是，重新生成外星人
    if len(aliens)<=0:
        bullets.empty()
        ai_settings.increase_speed()
        stats.level = stats.level+1
        sb.prep_level()
        create_fleet(ai_settings,screen,ship,aliens)


def update_bulltes(ai_settings,screen,stats,sb,ship,aliens,bullets):
    #更新子弹
    #bullets.update()
    for bullet in bullets:
        bullet.update()
    #删除子弹
    for bullet in bullets.copy():
        if bullet.rect.bottom<0:
            bullets.remove(bullet)
    #封装后的方法，实现碰撞检测和 生成外星人
    check_bullet_aliens_conllisions(ai_settings,screen,stats,sb,ship,aliens,bullets)

#获取一行有几个外星人的方法
def get_number_col(ai_settings,alien_width):
    #可用空间x 总长
    available_space_x = ai_settings.screen_width - (alien_width*2)
    #一行画几个
    num_ailen_x = available_space_x/(alien_width*2)
    return num_ailen_x
#计算总共有几行
def get_number_row(ai_settings,ship_height,alien_height):
    available_space_y = ai_settings.screen_height - ship_height - alien_height*3
    num_ailen_y = available_space_y/(alien_height*2)
    return num_ailen_y

#创建一个外星人
def create_alien(ai_settings,screen,aliens,alien_num_x,alien_num_y):
    alien = Ailen(ai_settings,screen)
    alien_width = alien.rect.width
    alien_height = alien.rect.height
    #设置一行外星人的位置
    alien.x =alien_width + alien_width*2*alien_num_x #
    alien.rect.x = alien.x
    alien.y =alien_height + alien_height*2*alien_num_y #
    alien.rect.y = alien.y
    aliens.add(alien)
#创建一群外星人
def create_fleet(ai_settings,screen,ship,aliens):
    alien = Ailen(ai_settings,screen)
    #获得一共有几列
    num_ailen_col =   get_number_col(ai_settings,alien.rect.width)
    #获得一共有几行
    num_ailen_row =   get_number_row(ai_settings,ship.rect.height,alien.rect.height)
    #创建一行外星人
    for row in range(0,int(num_ailen_row)):
        for col in range(0,int(num_ailen_col)):
            create_alien(ai_settings,screen,aliens,col,row)
#更新外星人
def update_aliens(ai_settings,stats,sb,screen,ship,aliens,bullets):
    #检查是否需要换方向
    check_feet_edges(ai_settings,aliens)
    #移动之前
    for alien in aliens:
        alien.update()
    if pygame.sprite.spritecollideany(ship,aliens):
        ship_hit(ai_settings,stats,sb,screen,ship,aliens,bullets)
    #检查外星人是否到达底部
    check_aliens_bottom(ai_settings,stats,sb,screen,ship,aliens,bullets)

def check_feet_edges(ai_settings,aliens):
    for alien in aliens:
        if alien.check_edges():
            change_feet_direction(ai_settings,aliens)
            break

#改变方向
def change_feet_direction(ai_settings,aliens):
    for alien in aliens:
        alien.rect.y =  alien.rect.y + ai_settings.feet_drop_speed
    #如何去反
    ai_settings.feet_direction =  ai_settings.feet_direction * -1

#碰到飞船，重开游戏
def ship_hit(ai_settings,stats,sb,screen,ship,aliens,bullets):
    if stats.ships_left>0:
        stats.ships_left = stats.ships_left-1
        sb.prep_ships()
    else:
        stats.game_active = False
        time.sleep(1)
    aliens.empty()
    bullets.empty()
    #创建新外星人
    create_fleet(ai_settings,screen,ship,aliens)
    ship.center_ship()
    #暂停0.5秒
    time.sleep(1)
#检查外星人是否到达底部
def check_aliens_bottom(ai_settings,stats,sb,screen,ship,aliens,bullets):
    screen_rect = screen.get_rect()
    for alien in aliens:
        if alien.rect.bottom >=screen_rect.bottom:
            ship_hit(ai_settings, stats,sb, screen, ship, aliens, bullets)
            break
#更新最高分
def check_height_score(stats,sb):
    if stats.score>stats.height_score:
        stats.height_score = stats.score
        sb.prep_height_score()


# 碰撞检测的方法(外星人,子弹)
# 遍历外星人
#   遍历子弹
#     碰撞检测的方法(外星人,子弹)
#        删除子弹
#        删除外星人



